//
//  DataManager.m
//  MobileZIPCodes
//
//  Created by Andreas Schwelling on 30.11.07.
//	http://www.andi.de/zipcodes/
// 
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.
//

#import "DataManager.h"

@implementation BrowseDataObject

-(void)initialize {
	
}

-(NSString*)getCountry {
	return _country;
}

-(void)setCountry:(NSString*)country {
	_country = country;
}

-(NSString*)getState {
	return _state;
}

-(void)setState:(NSString*)state {
	_state = state;
}

-(int)getType {
	return _browseType;
}

-(void)setType:(int)type {
	_browseType = type;
}

-(NSString*)getCity {
	return _city;
}

-(void)setCity:(NSString*)city {
	_city = city;
}


@end



@implementation DataManager


- (void) loadRemotePList {
	
	//NSLog(@"loading remote data.plist");
	_remoteplist = [[NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://www.andi.de/downloads/mobilezipcodes/data.plist"]] retain];
	
}

- (NSDictionary*) getRemoteData {
	
	return [_remoteplist objectForKey:@"availabledata"];
	
}

- (NSDictionary*) getLocalCountriesData {
	
	NSDictionary *plist = [self getPropertyList];
	return [plist objectForKey:@"availablecountries"];
	
	
}

- (NSString*) getFileSizeForCountry:(NSString*)country {
	
	
	NSDictionary *plist = [self getPropertyList];
	NSDictionary *plistdict = [plist objectForKey:@"availablecountries"];
	return [[plistdict objectForKey:country] objectForKey:@"filesize"];
	
	
}


- (NSMutableArray*) getLocalAvailableCountries {
	
	
	NSDictionary *plist = [self getPropertyList];
	
	//NSLog(@"getLocalAvailableC");
	NSDictionary *plistdict = [plist objectForKey:@"availablecountries"];
	NSMutableArray *retar = [[NSMutableArray alloc] init];
	[retar addObjectsFromArray:[[plist objectForKey:@"availablecountries"] allKeys]];
	return [retar autorelease];
	
}

- (NSMutableArray*) getRemoteAvailableCountries {
	
	//NSLog(@"getRemoteAvailableC");
	
	return [[_remoteplist objectForKey:@"availabledata"] allKeys];
	
}


- (NSDictionary*) getPropertyList {
	
	NSString *_appLibraryPath = [[self userLibraryDirectory] stringByAppendingPathComponent: @"ZIPcodes"];
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	[[NSFileManager defaultManager] createDirectoryAtPath: _appLibraryPath attributes: nil];
	
	NSDictionary *plistfile;
	
	if(![[NSFileManager defaultManager] fileExistsAtPath: _appSettingsPath])
	  {	
		  //NSLog(@"no plist found, new dict");
		  plistfile = [[NSMutableDictionary alloc] init];
	  } else 
		  plistfile =  [NSDictionary dictionaryWithContentsOfFile: _appSettingsPath];
	
	return plistfile;
	
	
	
}


- (void) setPListParam: (id) param key: (id)keyval
{
	
	NSMutableDictionary* preferencesDict = [self getPropertyList];
	
	[preferencesDict setObject:param forKey:keyval];
	
	NSString *_appLibraryPath = [[self userLibraryDirectory] stringByAppendingPathComponent: @"ZIPcodes"];
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	[[NSFileManager defaultManager] createDirectoryAtPath: _appLibraryPath attributes: nil];
	
	[preferencesDict writeToFile: _appSettingsPath atomically: YES];
}

- (id) getPListParam: (id) key 
{
	
	NSDictionary* preferencesDict = [self getPropertyList];
	return [preferencesDict objectForKey:key];
	
}


- (NSString*) getRemoteFileMirror:(NSString*)country {
	
	NSDictionary *plist = [self getPropertyList];
	
	NSDictionary *countries = [plist objectForKey:@"availablecountries"];
	//NSLog(@"country: %@",countries);
	NSDictionary *countrydict = [countries objectForKey:country];
	NSString *dbname = [[countrydict objectForKey:@"datasource"] objectForKey:@"mirror2"];
	return dbname;
	
	
}

- (void) deleteDatabaseFile:(NSString*)country {
	
	NSString *datab = [self getDataBaseName:country];
	[[NSFileManager defaultManager] removeFileAtPath: datab handler: nil];
	
}


- (NSString*) getRemoteFilesource:(NSString*)country {
	
	NSDictionary *plist = [self getPropertyList];
	
	NSDictionary *countries = [plist objectForKey:@"availablecountries"];
	//NSLog(@"country: %@",countries);
	NSDictionary *countrydict = [countries objectForKey:country];
	NSString *dbname = [[countrydict objectForKey:@"datasource"] objectForKey:@"mirror1"];
	return dbname;
	
	
}


- (NSString*) getDataBaseName:(NSString*)country {
	
	
	
	NSMutableString *filename = [NSMutableString stringWithString:country];
	[filename replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [filename length])];		
	filename = [[filename lowercaseString] stringByAppendingString:@".dat"];
	
	
	NSString *path = [@"/var/root/Media/ZIPcodes/" stringByAppendingString:filename];
	
	return path;
	
}

- (NSMutableArray*) getInstalledCountries {
	
	NSDictionary *plist = [self getPropertyList];
	
	NSString *_appLibraryPath = @"/var/root/Media/ZIPcodes/";
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	
	[[NSFileManager defaultManager] createDirectoryAtPath: _appLibraryPath attributes: nil];
	
	NSDictionary *plistfile;
	
	if(![[NSFileManager defaultManager] fileExistsAtPath: _appSettingsPath])
	  {	
		  //NSLog(@"no plist found, new dict");
		  plistfile = [[NSMutableDictionary alloc] init];
	  } else 
		  plistfile =  [NSDictionary dictionaryWithContentsOfFile: _appSettingsPath];
	
	if ([plistfile objectForKey:@"Countries"] != nil) {
		NSMutableArray *countries = [[NSMutableArray alloc] initWithArray:[[plistfile objectForKey:@"Countries"] allKeys]];
		[countries sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
		
		return [countries autorelease];
	} else {
		return nil;
	}
	
	
}

- (void) setInstalledCountries:(NSArray*)arr {
	
	NSDictionary *plist = [self getPropertyList];
	
	NSString *_appLibraryPath = @"/var/root/Media/ZIPcodes/";
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	
	[[NSFileManager defaultManager] createDirectoryAtPath: _appLibraryPath attributes: nil];
	
	NSMutableDictionary *plistfile;
	
	if(![[NSFileManager defaultManager] fileExistsAtPath: _appSettingsPath])
	  {	
		  //NSLog(@"no plist found, new dict");
		  plistfile = [[NSMutableDictionary alloc] init];
	  } else 
		  plistfile =  [NSMutableDictionary dictionaryWithContentsOfFile: _appSettingsPath];
	
	NSMutableDictionary *cdict = [[NSMutableDictionary alloc] init];
	
	int i=0;
	for(i=0;i<[arr count];i++) {
		
		NSMutableDictionary *innerdict = [[NSMutableDictionary alloc] init];
		
		NSMutableString *filename = [NSMutableString stringWithString:[arr objectAtIndex:i]];
		[filename replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [filename length])];		
		filename = [[filename lowercaseString] stringByAppendingString:@".dat"];
		[innerdict setObject:filename forKey:@"filename"];
		[cdict setObject:innerdict forKey:[arr objectAtIndex:i]];
		[innerdict release];
	}
	
	//NSLog(@"dict: %@",cdict);
	
	
	
	[plistfile setObject:cdict forKey:@"Countries"];
	[cdict release];
	[plistfile writeToFile: _appSettingsPath atomically: YES];
}


-(BOOL) isFavouredEntry:(NSString*) city forCountry:(NSString*)country {
	
	NSDictionary *plist = [self getPropertyList];
	//NSLog(@"plist: %@",plist);
	
	NSMutableDictionary *favourdict = [plist objectForKey:@"favourites"];
	if (favourdict == nil) favourdict = [[NSMutableDictionary alloc] init];
	//NSLog(@"favourdict: %@",favourdict);
	
	NSMutableDictionary *countryfavdict = [favourdict objectForKey:country];
	if (countryfavdict == nil) countryfavdict = [[NSMutableDictionary alloc] init];
	
	NSMutableDictionary *citydict = [countryfavdict objectForKey:city];
	
	if (citydict != nil) return YES; else return NO;
	
	
}

-(void) removeFavouredEntry:(NSString*) city forCountry:(NSString*)country {
	
	NSDictionary *plist = [self getPropertyList];
	//NSLog(@"plist: %@",plist);
	
	NSMutableDictionary *favourdict = [plist objectForKey:@"favourites"];
	if (favourdict == nil) favourdict = [[NSMutableDictionary alloc] init];
	//NSLog(@"favourdict: %@",favourdict);
	
	NSMutableDictionary *countryfavdict = [favourdict objectForKey:country];
	if (countryfavdict == nil) countryfavdict = [[NSMutableDictionary alloc] init];
	//NSLog(@"countryfavdict: %@",countryfavdict);
	
	[countryfavdict removeObjectForKey:city];
	
	[favourdict setObject:countryfavdict forKey:country];
	
	
	//NSLog(@"favourdict: %@",favourdict);
	
	[plist setObject:favourdict forKey:@"favourites"];
	
	NSString *_appLibraryPath = [[self userLibraryDirectory] stringByAppendingPathComponent: @"ZIPcodes"];
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	
	//NSLog(@"appsetpath: %@",_appSettingsPath);
	
	[plist writeToFile: _appSettingsPath atomically: YES];
	
	
	
}


- (void) addFavouredEntry:(NSString*) city withState:(NSString*)state forCountry:(NSString*)country {
	
	NSDictionary *plist = [self getPropertyList];
	//NSLog(@"plist: %@",plist);
	
	NSMutableDictionary *favourdict = [plist objectForKey:@"favourites"];
	if (favourdict == nil) favourdict = [[NSMutableDictionary alloc] init];
	//NSLog(@"favourdict: %@",favourdict);
	
	NSMutableDictionary *countryfavdict =  [[NSMutableDictionary alloc] init];
	[countryfavdict addEntriesFromDictionary:[favourdict objectForKey:country]];
	
	//NSLog(@"countryfavdict: %@",countryfavdict);
	
	
	NSMutableDictionary *entrydict = [[NSMutableDictionary alloc] init];
	[entrydict setObject:city forKey:@"city"];
	[entrydict setObject:state forKey:@"state"];
	
	[countryfavdict setObject:entrydict forKey:city];
	
	
	[favourdict setObject:countryfavdict forKey:country];
	
	
	//NSLog(@"favourdict: %@",favourdict);
	
	[plist setObject:favourdict forKey:@"favourites"];
	
	NSString *_appLibraryPath = [[self userLibraryDirectory] stringByAppendingPathComponent: @"ZIPcodes"];
	NSString *_appSettingsPath = [_appLibraryPath stringByAppendingPathComponent: @"Settings.plist"];
	
	//NSLog(@"appsetpath: %@",_appSettingsPath);
	
	[plist writeToFile: _appSettingsPath atomically: YES];
	
	
}

- (NSMutableArray*) getFavouredEntriesCountries {
	
	NSDictionary *plist = [self getPropertyList];
	//NSLog(@"plist: %@",plist);
	
	NSMutableDictionary *favourdict = [plist objectForKey:@"favourites"];
	if (favourdict == nil) favourdict = [[NSMutableDictionary alloc] init];
	
	NSMutableArray *retar = [[NSMutableArray alloc] init];
	
	[retar addObjectsFromArray:[favourdict allKeys]];
	
	return [retar autorelease];
	
}

- (NSDictionary*) getFavouredEntriesForCountry: (NSString*) country {
	
	NSDictionary *plist = [self getPropertyList];
	//NSLog(@"plist: %@",plist);
	
	NSMutableDictionary *favourdict = [plist objectForKey:@"favourites"];
	if (favourdict == nil) favourdict = [[NSMutableDictionary alloc] init];
	
	NSDictionary *retdict = [favourdict objectForKey:country];
	
	return retdict;
	
	
}




@end
